package advance.app.controller;

import advance.app.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : fafa
 * @version V1.0
 * @Project: demo
 * @Package advance.app.controller
 * @Description:
 * @date Date : 2019年11月19日 09:53
 */
@Controller
@CrossOrigin
@RequestMapping("/")
public class AdminController {

    @RequestMapping("/admin")
    public String admin(){
        return "admin";
    }

    @RequestMapping("/login")
    public String loginPage(){
        return "admins/login";
    }

    @RequestMapping(value = "/user/login", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> login(@RequestBody User user,HttpServletRequest request) throws Exception  {
        System.out.println(user.toString());
        Map<String,Object> result = new HashMap<>();
        HttpSession session = request.getSession(true);

        if("admin".equals(user.getUsername()) && "12345.com".equals(user.getPassword())){
            session.setAttribute("loginUser", "admin");
            result.put("status",true);
            result.put("msg","success");
            return result;
        }
        result.put("status",false);
        result.put("msg","fail");
        return result;
    }

    @RequestMapping("/admin/add")
    public String addFile() {
        return "admins/add";
    }

    @RequestMapping("/admin/updhis")
    public String updHis() {
        return "admins/updhis";
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("loginUser");
        return "login";
    }
}
